package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsGoodsSpecificationMapper;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 14:37
 */
@Service
public class DtsGoodsSpecificationServiceImpl implements DtsGoodsSpecificationService {
    @Autowired
    private DtsGoodsSpecificationMapper dtsGoodsSpecificationMapper;
    @Override
    public List<DtsGoodsSpecification> selectByGoodsId(Integer id) {
        return  dtsGoodsSpecificationMapper.selectList(new QueryWrapper<DtsGoodsSpecification>().eq("goods_id",id).eq("deleted",false));
    }

    @Override
    public void update(List<DtsGoodsSpecification> specifications, Integer goodsId) {
        List<DtsGoodsSpecification> goodsSpecifications = dtsGoodsSpecificationMapper.selectList(new QueryWrapper<DtsGoodsSpecification>().eq("goods_id", goodsId).eq("deleted",false));
        ArrayList<DtsGoodsSpecification> insertGoodsSpecifications = new ArrayList<>();
        ArrayList<DtsGoodsSpecification> deleteGoodsSpecifications = new ArrayList<>();
        for (DtsGoodsSpecification specification : specifications) {

            if(!goodsSpecifications.contains(specification)){
                specification.setGoodsId(goodsId);
                insertGoodsSpecifications.add(specification);
            }
        }
        dtsGoodsSpecificationMapper.insertList(insertGoodsSpecifications);
        for (DtsGoodsSpecification goodsSpecification : goodsSpecifications) {
            if(!specifications.contains(goodsSpecification)){
                deleteGoodsSpecifications.add(goodsSpecification);
            }
        }
        dtsGoodsSpecificationMapper.updateList(deleteGoodsSpecifications);
    }

    @Override
    public void insert(List<DtsGoodsSpecification> specifications) {
        dtsGoodsSpecificationMapper.insertList((ArrayList<DtsGoodsSpecification>) specifications);
    }
}
